package com.bls.cate.ui.activity.person_info;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bls.blslib.base.BaseActivity;
import com.bls.blslib.eventbus.Event;
import com.bls.cate.R;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.model.LoginSpRes;
import com.bls.cate.ui.activity.person_info_change.PersonInfoChangeActivity;
import com.bls.cate.ui.activity.person_info_change.SetHeaderActivity;
import com.bls.cate.utils.AccountUtil;
import com.bls.cate.utils.FrescoImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/5.
 */

public class PersonInfoActivity extends BaseActivity {

    private ImageView btnBack;
    private LinearLayout btnHeader, btnLlNickName, btnLlSex, btnLlBirth, btnLlConstellation;
    private SimpleDraweeView ivHeader;
    private TextView tvTitle, tvName, tvSex, tvBirth, tvConstellation;

    private PersonInfoActivity mActivity;
    private LoginSpRes loginSpRes;
    private String[][] constellations;
    private int[] constellationDate;

    @Override
    protected void initParams() {
        mActivity = (PersonInfoActivity) getActivity();
        constellations = new String[][]{{"摩羯座", "水瓶座"}, {"水瓶座", "双鱼座"}, {"双鱼座", "白羊座"}, {"白羊座", "金牛座"}, {"金牛座", "双子座"}, {"双子座", "巨蟹座"}, {"巨蟹座", "狮子座"},
                {"狮子座", "处女座"}, {"处女座", "天秤座"}, {"天秤座", "天蝎座"}, {"天蝎座", "射手座"}, {"射手座", "摩羯座"}};
//星座分割时间
        constellationDate = new int[]{20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_person_info;
    }

    @Override
    protected void initFvb() {
        tvTitle = findViewById(R.id.title_middle_text);
        btnBack = findViewById(R.id.title_left_iv_2);
        btnBack.setVisibility(View.VISIBLE);
        btnHeader = findViewById(R.id.btn_ll_header);
        ivHeader = findViewById(R.id.iv_header);
        btnLlNickName = findViewById(R.id.btn_ll_nickname);
        btnLlSex = findViewById(R.id.btn_ll_sex);
        btnLlBirth = findViewById(R.id.btn_ll_birth);
        btnLlConstellation = findViewById(R.id.btn_ll_constellation);
        tvName = findViewById(R.id.tv_name);
        tvSex = findViewById(R.id.tv_sex);
        tvBirth = findViewById(R.id.tv_birth);
        tvConstellation = findViewById(R.id.tv_constellation);
    }

    @Override
    protected void initView() {
        super.initView();
        tvTitle.setText("个人资料");
    }

    @Override
    protected void initListener() {
        setOnClickListener(btnBack, btnHeader, btnLlNickName, btnLlSex, btnLlBirth);
    }

    @Override
    protected void initData() {
        loginSpRes = AccountUtil.getStrLoginSp();
        if (loginSpRes != null) {
            FrescoImageLoader.showImage(ivHeader, loginSpRes.getHeaderImgMiddle());
            tvName.setText(loginSpRes.getNickName());
            int tempSex = loginSpRes.getSex();
            tvSex.setText(tempSex == 0 ? "男" : (tempSex == 1 ? "女" : ""));
            if (loginSpRes.getBirthday().length() > 0) {
                tvBirth.setText(loginSpRes.getBirthday());
                tvConstellation.setText(getConstellations(loginSpRes.getBirthday()));
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_iv_2:
                finish();
                break;
            case R.id.btn_ll_header:
                Intent intentHeader = new Intent(mActivity, SetHeaderActivity.class);
                intentHeader.putExtra("UniqueId", loginSpRes.getUniqueId());
                intentHeader.putExtra("HeaderImg", loginSpRes.getHeaderImgLarge());
                mActivity.startActivity(intentHeader);
                break;
            case R.id.btn_ll_nickname:
                Intent intentNickName = new Intent(mActivity, PersonInfoChangeActivity.class);
                intentNickName.putExtra("UniqueId", loginSpRes.getUniqueId());
                intentNickName.putExtra("Type", "NickName");
                intentNickName.putExtra("Value", loginSpRes.getNickName());
                mActivity.startActivity(intentNickName);
                break;
            case R.id.btn_ll_sex:
                Intent intentSex = new Intent(mActivity, PersonInfoChangeActivity.class);
                intentSex.putExtra("UniqueId", loginSpRes.getUniqueId());
                intentSex.putExtra("Type", "Sex");
                intentSex.putExtra("Value", loginSpRes.getSex());
                mActivity.startActivity(intentSex);
                break;
            case R.id.btn_ll_birth:
                Intent intentBirth = new Intent(mActivity, PersonInfoChangeActivity.class);
                intentBirth.putExtra("UniqueId", loginSpRes.getUniqueId());
                intentBirth.putExtra("Type", "Birthday");
                intentBirth.putExtra("Value", loginSpRes.getBirthday());
                mActivity.startActivity(intentBirth);
                break;
        }
    }

    //星座生成 传进是日期格式为: yyyy-mm-dd
    private String getConstellations(String birthday) {
        String[] data = birthday.split("-");
        int day = constellationDate[Integer.parseInt(data[1]) - 1];
        String[] cl1 = constellations[Integer.parseInt(data[1]) - 1];
        if (Integer.parseInt(data[2]) >= day) {
            // become_constellation.setText(cl1[1]);
            return cl1[1];
        } else {
            return cl1[0];
            // become_constellation.setText(cl1[0]);
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveStickyEvent(Event event) {
        super.receiveStickyEvent(event);
        if (event.getCode() == EventBusCode.ChangeUserInfo_Refresh) {
            initData();
        }
    }
}
